package sos.etc;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class MapReduceGet {

    public static class MapReduceGetMap extends Mapper<LongWritable, Text, Text, NullWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String s = value.toString();
            boolean a = s.contains("img src=");
            boolean F = s.contains("width");
            if (a && F) {
                String[] line = s.split("img src");
                String b = line[1];
                String[] c = b.split("width");
                String d = c[0];

                context.write(new Text(d), NullWritable.get());
            } else {
                return;
            }
        }


        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
            Configuration conf = new Configuration();
            //配置作业名
            Job job = Job.getInstance(conf);
            //配置作业各个类
            job.setJarByClass(MapReduceGet.class);

            job.setMapperClass(MapReduceGetMap.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(NullWritable.class);

            File file = new File("F:\\tp");
            if (file.exists()) {
                FileUtils.deleteDirectory(file);
            }


            FileInputFormat.addInputPath(job, new Path("F:\\tp.txt"));
            FileOutputFormat.setOutputPath(job, new Path("F:\\tp"));

            job.setNumReduceTasks(1);
            job.waitForCompletion(true);
        }

    }
}
